#! /usr/bin/env python3
"""
ORM for the participators.

2019-3-23 by Stone at GDOU-KC
"""

import os
import logging
import peewee
from peewee import CharField, BooleanField

# ===== ===== ===== =====
# Constants
# ===== ===== ===== =====
# DEFAULT_CHAR = 'Null'  # default value for uninitialized model of CHAR-like type
IF_CREATE_TABLE = True
DEFAULT_CHAR = u'无记录'
DB_PATH = os.path.join('db', 'Person.sqlite')


# ===== ===== ===== =====
# Class definitions
# ===== ===== ===== =====
db = peewee.SqliteDatabase(DB_PATH)


class BaseModel(peewee.Model):
    class Meta:
        database = db


class Person(BaseModel):
    open_id = CharField(null=True, unique=True)
    xuehao = CharField(unique=True, default=DEFAULT_CHAR)
    name = CharField(null=True, default=DEFAULT_CHAR)
    phone_number = CharField(null=True, default=DEFAULT_CHAR)
    major_class = CharField(null=True, default=DEFAULT_CHAR)  # the major and class of this participator
    team_name = CharField(null=True, default=DEFAULT_CHAR)  # the name of the team which this participator is belonged to
    isCaptain = BooleanField(default=False)  # If this participator is captain


# ===== ===== ===== =====
# Run
# ===== ===== ===== =====
if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    if IF_CREATE_TABLE:
        db.connect()
        db.create_tables([Person])
        logging.info("\r\n============Table created !============ \r\n")
        db.close()

    p = Person(xuehao='xh2')
    p.p2333 = 2333
    p.save()
